import 'package:fate_blind_box/config/app_style.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MeetAppBar extends StatelessWidget implements PreferredSizeWidget {
  final String? title;
  final Widget? rightBtn;
  final bool centerTitle;
  final bool? showBottom;
  final GestureTapCallback? onTap;
  final VoidCallback? back;
  final SystemUiOverlayStyle? systemOverlayStyle;
  final Color backgroundColor;

  const MeetAppBar({
    Key? key,
    this.title,
    this.rightBtn,
    this.centerTitle = true,
    this.showBottom,
    this.onTap,
    this.back,
    this.systemOverlayStyle,
    this.backgroundColor = Colors.white,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AppBar(
      elevation: 0,
      systemOverlayStyle: SystemUiOverlayStyle.dark,
      leading: IconButton(
          onPressed: () {
            if (back != null) {
              back!();
            } else {
              Navigator.of(context).pop();
            }
          },
          icon: Icon(
            Icons.arrow_back_ios,
            color: '#333333'.stringToColor,
          )),
      titleSpacing: 0,
      title: Text(
        title ?? '未定义',
        style: TextStyle(
            color: const Color(0xff333333),
            fontSize: 17.sp,
            fontWeight: FontWeight.w600),
      ),
      centerTitle: centerTitle,
      actions: [
        Row(
          children: [
            rightBtn ?? const SizedBox(),
          ],
        ),
      ],
      backgroundColor: backgroundColor,
      bottom: PreferredSize(
          preferredSize: Size(double.infinity, bottomHeight()),
          child: showBottom == true
              ? InkWell(
                  onTap: () => onTap != null ? onTap!() : null,
                  child: Container(
                    margin:
                        EdgeInsets.symmetric(horizontal: 10.w, vertical: 4.w),
                    padding:
                        EdgeInsets.symmetric(horizontal: 14.w, vertical: 2.w),
                    height: 32.w,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(20.w),
                      color: const Color(0xffEEEEEE),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset(
                          'assets/images/search.png',
                          fit: BoxFit.contain,
                          width: 18.w,
                          height: 18.w,
                          package: 'ahu_pub_lib',
                        ),
                        Text('搜索',
                            style: TextStyle(
                                color: const Color(0xffAAAAAA),
                                fontSize: 14.sp,
                                fontWeight: FontWeight.w400)),
                      ],
                    ),
                  ),
                )
              : const SizedBox()),
    );
  }

  @override
  // TODO: implement preferredSize
  Size get preferredSize => Size.fromHeight(kToolbarHeight + bottomHeight());

  double bottomHeight() {
    if (showBottom == true) {
      return ScreenUtil().statusBarHeight;
    }
    return 0.w;
  }
}
